package com.cryptoneedle.center.doris.service.doris;

import cn.hutool.v7.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.bubbles.engine.data.core.repository.BaseRepository;
import com.bubbles.engine.data.core.service.BaseService;
import com.cryptoneedle.center.common.entity.doris.DorisDatabase;
import com.cryptoneedle.center.common.key.DatabaseKey;
import com.cryptoneedle.center.common.repository.doris.DorisDatabaseRepository;
import com.cryptoneedle.center.doris.repository.metadata.DorisMetadataRepository;
import com.google.common.collect.Maps;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>description: Doris Database </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Service
@DS("pg")
@Slf4j
public class DorisDatabaseService extends BaseService<DorisDatabase, DatabaseKey> {

    @Resource
    private DorisDatabaseService dorisDatabaseService;

    @Resource
    private DorisDatabaseRepository dorisDatabaseRepository;

    @Resource
    private DorisMetadataRepository dorisMetadataRepository;


    @Override
    public BaseRepository<DorisDatabase, DatabaseKey> getRepository() {
        return this.dorisDatabaseRepository;
    }


    @Transactional
    public List<DorisDatabase> list() {
        return dorisDatabaseRepository.list();
    }

    @Transactional
    public List<DorisDatabase> listByCatalog(String catalogName) {
        return dorisDatabaseRepository.listByCatalog(catalogName);
    }

    @Transactional
    public void addList(List<DorisDatabase> list) {
        dorisDatabaseRepository.saveAll(list);
    }

    @Transactional
    public void saveList(List<DorisDatabase> list) {
        dorisDatabaseRepository.saveAll(list);
    }

    @Transactional
    public void deleteList(List<DorisDatabase> list) {
        dorisDatabaseRepository.deleteAll(list);
    }

    public List<DorisDatabase> syncDorisDatabase(String catalogName) {
        // 查询所有
        List<DorisDatabase> originList = dorisDatabaseService.listByCatalog(catalogName);
        Map<String, DorisDatabase> originMap = Maps.uniqueIndex(originList, DorisDatabase::getKeyString);

        // 待同步数据
        List<DorisDatabase> dealList = dorisMetadataRepository.showDatabasesFrom(catalogName)
                .stream()
                .map(deal -> BeanUtil.copyProperties(deal, DorisDatabase.class))
                .toList();
        Map<String, DorisDatabase> dealMap = Maps.uniqueIndex(dealList, DorisDatabase::getKeyString);

        // 新增数据
        List<DorisDatabase> addList = dealList.stream().filter(deal -> !originMap.containsKey(deal.getKeyString())).toList();

        // 移除数据
        List<DorisDatabase> removeList = originList.stream().filter(item -> !dealMap.containsKey(item.getKeyString())).toList();

        // 保存数据
        List<DorisDatabase> saveList = dealList
                .stream()
                .filter(deal -> originMap.containsKey(deal.getKeyString()))
                .peek(deal -> {
                    DorisDatabase origin = originMap.get(deal.getKeyString());
                }).toList();

        dorisDatabaseService.addList(addList);
        dorisDatabaseService.saveList(saveList);
        dorisDatabaseService.deleteList(removeList);

        return dorisDatabaseService.list();
    }
}